1893D - Colorful Constructive - CodeForces Solution


constructive algorithms data structures greedy *2600

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int n,m,a[200005],b[200005],c[200005],vis[200005],id[200005];
map<int,int> mp[200005];
struct node{
	int id,lim,cnt;
	bool operator <(const node &b) const
	{
		return min(lim,cnt)<min(b.lim,b.cnt);
	}
};
void output(int x)
{
	priority_queue<pair<int,int> > q;
	for(auto i:mp[x]) q.emplace(i.second,i.first)/*,cerr<<x<<" "<<i.first<<" "<<i.second<<"\n"*/;
	vector<pair<int,int> > f(b[x]+5);
	for(int i=1;i<=b[x];i++)
	{
		if(f[i].first!=0) q.emplace(f[i]);
		auto j=q.top();
		q.pop();
		cout<<j.second<<" ";
		j.first--;
		if(j.first!=0&&i+c[x]<=b[x]) f[i+c[x]]=j; 
	}
	cout<<"\n";
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
			vis[a[i]]++;
		}
		for(int i=1;i<=m;i++)
		{
			cin>>b[i];
		}
		for(int i=1;i<=m;i++)
		{
			cin>>c[i];
			// cerr<<"lim:"<<c[i]<<"\n";
		}
		set<pair<int,int> > s;
		for(int i=1;i<=n;i++)
		{
			if(vis[i]) s.insert({vis[i],i});
		}
		priority_queue<node> q;
		for(int i=1;i<=m;i++)
		{
			q.push({i,c[i],b[i]});
		}
		bool f=1;
		while(!q.empty())
		{
			auto [x,y,z]=q.top();//找出限制最大的架子
			q.pop();
			if(min(y,z)>s.size())
			{
				f=0;
				break ;
			}
			vector<pair<int,int> > _;
			for(int i=1;i<=min(y,z);i++)
			{
				auto it=prev(s.end());//优先把出现次数最多的分配给他
				auto t=*it;
				t.first--;
				_.emplace_back(t);
				mp[x][t.second]++;
				s.erase(it);
			}
			z-=min(y,z);
			if(z) q.push({x,y,z});
			for(auto i:_)
			{
				if(i.first) s.insert(i);
			}
		}
		if(!f) cout<<"-1\n";
		else
		{
			for(int i=1;i<=m;i++)
			{
				output(i);	
			}
		}
		for(int i=1;i<=m;i++)
		{
			mp[i].clear();
		}
		for(int i=1;i<=n;i++)
		{
			vis[i]=0;
		}
	}
}


Comments

Submit
0 Comments
More Questions

1169B - Pairs
1567D - Expression Evaluation Error
78A - Haiku
1287A - Angry Students
1428A - Box is Pull
234B - Reading
581B - Luxurious Houses
1481C - Fence Painting
935A - Fafa and his Company
22A - Second Order Statistics
1720B - Interesting Sum
1720A - Burenka Plays with Fractions
3A - Shortest path of the king
1720C - Corners
574A - Bear and Elections
352B - Jeff and Periods
1244A - Pens and Pencils
1670A - Prof Slim
1189A - Keanu Reeves
678A - Johny Likes Numbers
1699C - The Third Problem
1697D - Guess The String
754B - Ilya and tic-tac-toe game
760A - Petr and a calendar
1573A - Countdown
166A - Rank List
1631B - Fun with Even Subarrays
727A - Transformation from A to B
822B - Crossword solving
1623A - Robot Cleaner